//
//  CAAccountSettingVC.m
//  CleanerAir_Project
//
//  Created by 付亚杰 on 16/6/27.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "CAAccountSettingVC.h"
#import "UMSocialSnsPlatformManager.h"
//#import "UMSocialAccountEntity.h"
#import "UMSocialAccountManager.h"

@interface CAAccountSettingVC ()

@property (weak, nonatomic) IBOutlet UIView *phone;
@property (weak, nonatomic) IBOutlet UIView *weixin;
@property (weak, nonatomic) IBOutlet UIView *weibo;
@property (weak, nonatomic) IBOutlet UIView *QQ;

@property (weak, nonatomic) IBOutlet UILabel *phoneLable;
@property (weak, nonatomic) IBOutlet UILabel *weixinLabel;
@property (weak, nonatomic) IBOutlet UILabel *weiboLabel;
@property (weak, nonatomic) IBOutlet UILabel *qqLabel;


@end

@implementation CAAccountSettingVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     self.titlelabel.text = @"帐号设置";
    
    
    [self.phone addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneClick)]];
    [self.weixin addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wweixinClick)]];
    [self.weibo addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(weiboClick)]];
    [self.QQ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(QQClick)]];
    
    [self getOauthInfo];
    // Do any additional setup after loading the view from its nib.
}

- (void)getOauthInfo{

    
    if ([LoginSingleton sharedLoginSingleton].perInfoModel.o_qq.integerValue == 1) {
        self.qqLabel.text = @"已绑定";
        self.qqLabel.textColor = [UIColor blackColor];
    }
    if ([LoginSingleton sharedLoginSingleton].perInfoModel.o_sina.integerValue == 1) {
        self.weiboLabel.textColor = [UIColor blackColor];
        self.weiboLabel.text = @"已绑定";
    }
    if([LoginSingleton sharedLoginSingleton].perInfoModel.o_phone.integerValue == 1){
        self.phoneLable.text = @"已绑定";
        self.phoneLable.textColor = [UIColor blackColor];
    }
    
    if ([LoginSingleton sharedLoginSingleton].perInfoModel.o_wchat.integerValue == 1) {
        self.weixinLabel.text = @"已绑定";
        self.weixinLabel.textColor = [UIColor blackColor];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)phoneClick{
    
}

- (void)wweixinClick{
    
    __weak __typeof(self) weakSelf = self;
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToWechatSession];
            
            //            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            //
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId);
            [weakSelf thirdPartCertificationWithInfo:snsAccount.usid withType:@"4"];
            
        }
        
    });
    
}

- (void)weiboClick{
    __weak __typeof(self) weakSelf = self;
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [weakSelf thirdPartCertificationWithInfo:snsAccount.usid withType:@"3"];
            
        }});
    
}

- (void)QQClick{
    
    __weak __typeof(self) weakSelf = self;
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        //        DLog(@"结果：%u",response.responseCode);
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            [weakSelf thirdPartCertificationWithInfo:snsAccount.usid withType:@"2"];
            
            
        }});
    
}


#pragma mark -- 三方认证
- (void)thirdPartCertificationWithInfo:(NSString *)response withType:(NSString *)loginType{
    
    [self showIndicator:@"正在认证..."];
    
    NSString *ids = [LoginSingleton sharedLoginSingleton].loginModel.ids;
    NSString *token = [LoginSingleton sharedLoginSingleton].token;
    
    __typeof(self) weakSelf = self;
    [HTTPManager post:GET_URLString(@"Api/Member/oauth_member") parameters:@{@"id":ids,@"token":token,@"type":loginType,@"msg":@"",@"value":response} success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        [weakSelf hideIndicator];
        if ([responseObject[@"msg"] isEqualToString:@"成功"]) {
            
            [ShowMessage showCenterViewMessage:@"绑定成功"];
            
            switch (loginType.integerValue) {
                case 1:
                    [LoginSingleton sharedLoginSingleton].perInfoModel.o_phone = @"1";
                    break;
                case 2:
                    [LoginSingleton sharedLoginSingleton].perInfoModel.o_qq = @"1";
                    break;
                case 3:
                    [LoginSingleton sharedLoginSingleton].perInfoModel.o_sina = @"1";
                    break;
                case 4:
                    [LoginSingleton sharedLoginSingleton].perInfoModel.o_wchat = @"1";
                    break;
                default:
                    break;
            }
            
            [weakSelf getOauthInfo];
            
        }else{
            [ShowMessage showCenterViewMessage:responseObject[@"msg"]];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [weakSelf hideIndicator];
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
